package com.qdu.servlet;

import com.google.gson.Gson;
import com.oreilly.servlet.MultipartRequest;
import com.qdu.util.CustomFileRenamePolicy;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 该Servlet负责上传多张图片的请求
 *
 * @author Anna
 */
@WebServlet("/uploadServlet")
public class UploadServlet extends HttpServlet {

    private String save_directory = "D:/upload";

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        CustomFileRenamePolicy policy = new CustomFileRenamePolicy();

        MultipartRequest mreq = new MultipartRequest(req, save_directory, 100 * 1024 * 1024, "utf-8", policy);

        List<String> fileNameList = policy.getFileNames();

        System.out.println("---------------------------------------------------");
        for (String name : fileNameList) {
            System.out.println(name);
        }
        System.out.println("---------------------------------------------------");
        
        Map result=new HashMap();
        result.put("errno", 0);
        result.put("data",fileNameList);
        
        Gson gson=new Gson();
        String jsonObject=gson.toJson(result);
        
        resp.setContentType("application/x-json;charset=utf-8");
        System.out.println(jsonObject);
        resp.getWriter().println(jsonObject);
        
    }
}
